package com.thunder.soft.ui.activity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;

import com.thunder.soft.DownloadService;
import com.thunder.soft.R;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivityNotifyTestBinding;
import com.thunder.soft.vm.NotifyTestViewModel;


public class NotifyTestActivity extends BaseActivity<ActivityNotifyTestBinding> {

    private NotifyTestViewModel viewModel;


    private DownloadService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder = (DownloadService.DownloadBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent=new Intent(this,DownloadService.class);
        startService(intent);
        bindService(intent,connection,BIND_ABOVE_CLIENT);
        if(ContextCompat.checkSelfPermission(NotifyTestActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(NotifyTestActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }

    @Override
    protected void initData() {
        viewModel = ViewModelProviderHelper.buildViewModel(this, NotifyTestViewModel.class);
        getLifecycle().addObserver(viewModel);
        binding.setViewModel(viewModel);

        viewModel.getFinishActivity().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                finish();
            }
        });

        viewModel.getLiveData().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer) {
                    case 0:
                        String url = "https://www.wanandroid.com/blogimgs/aa0da019-c965-4c8a-8919-198397e0c3f1.apk";
                        downloadBinder.startDownload(url);
                        break;
                    case 1:
                        downloadBinder.pauseDownload();
                        break;
                    case 2:
                        downloadBinder.cancelDownload();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_notify_test;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}